#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""

@author ：chenzf
@date ：2024/4/22 下午8:52 
"""
import json

from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
from pyecharts import options as  opts

from data_demo.my_data import Record


class FileReader:
    def __init__(self, path):
        self.path = path  # 文件路径

    def read_data(self) -> list[Record]:
        pass


class TextFileReader(FileReader):
    def read_data(self) -> list[Record]:
        file = open(self.path, "r", encoding="utf-8")
        record_list: list[Record] = []
        for line in file.readlines():
            line = line.strip()  # 去除空行换行符
            if not line:  # 等于 java 中的 !(false)
                continue
            line_data = line.split(",")
            record = Record(line_data[0], line_data[1], line_data[2], line_data[3])
            record_list.append(record)
        file.close()
        return record_list


class JsonFileReader(FileReader):
    def read_data(self) -> list[Record]:
        file = open(self.path, "r", encoding="utf-8")
        record_list: list[Record] = []
        for line in file.readlines():
            line = line.strip()  # 去除空行换行符
            if not line:  # 等于 java 中的 !(false)
                continue
            json_data = json.loads(line)
            record = Record(json_data["date"], json_data["order_id"], json_data["money"], json_data["province"])
            record_list.append(record)
        file.close()
        return record_list


# if __name__ == '__main__':
#     reader = TextFileReader("./sale_data.txt")
#     data = reader.read_data()
#     for datum in data:
#         print(datum)
#     print()
#     reader = JsonFileReader("./sale_json_data.txt")
#     data = reader.read_data()
#     for datum in data:
#         print(datum)
#     # 数据转换
#     # json_file = open("./sale_json_data.txt", "w", encoding="utf-8")
#     # for datum in data:
#     #     json_file.write(json.dumps(datum.__dict__, ensure_ascii=False) + "\n")
#     # json_file.close()


# class file_demo:
    # text_data = TextFileReader("./sale_data.txt").read_data()
    # json_data = JsonFileReader("./sale_json_data.txt").read_data()
    # text_data += json_data
    # data_dict = {}
    # for datum in text_data:
    #     if datum.date in data_dict.keys():
    #         data_dict[datum.date] += float(datum.money)
    #     else:
    #         data_dict[datum.date] = float(datum.money)
    # #print(data_dict)
    # bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))# 设置主题
    # bar.add_xaxis(list(data_dict.keys())) # 设置x轴
    # bar.add_yaxis("销售额", list(data_dict.values()),label_opts=opts.LabelOpts(is_show=False)) # 设置y轴
    # bar.set_global_opts(title_opts=opts.TitleOpts(title="每日销售额"))# 设置标题
    # bar.render("每日销售额统计柱状图.html")# 渲染 并 保存

